//
//  AppDelegate.m
//  RuntimeDemo
//
//  Created by LMM on 7/20/15.
//  Copyright (c) 2015 LMM. All rights reserved.
//

#import "AppDelegate.h"
#import "APService.h"
#import "MobClick.h"
#import "MobClickSocialAnalytics.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) {
        [APService registerForRemoteNotificationTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
    }else {
        [APService registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert categories:nil];
    }
    
    
#else
    [APService registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert categories:nil];

    
#endif
    [APService setupWithOption:launchOptions];
    NSDictionary *remoteNotification = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    NSLog(@"avtive:%@", remoteNotification);
    if ([remoteNotification isKindOfClass:[NSDictionary class]]) {
        NSString *string = [NSString stringWithFormat:@"%d", [remoteNotification[@"badge"] intValue]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:remoteNotification[@"alert"] message:string delegate:self cancelButtonTitle:remoteNotification[@"sound"] otherButtonTitles:@"active", nil];
        [alert show];
    }
    // 设置UM
    [MobClick startWithAppkey:@"55bce9f567e58e38e300240d" reportPolicy:BATCH channelId:nil];
    NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    [MobClick setVersion:[version integerValue]];
    [MobClick setEncryptEnabled:NO];
    [MobClick setBackgroundTaskEnabled:YES];
    [MobClick setCrashReportEnabled:YES];
    
    
    return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [APService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    
    [APService handleRemoteNotification:userInfo];
    NSDictionary *remoteNotification = userInfo[@"aps"];
    NSString *string = [NSString stringWithFormat:@"%d", [remoteNotification[@"badge"] intValue]];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:remoteNotification[@"alert"] message:string delegate:self cancelButtonTitle:remoteNotification[@"sound"] otherButtonTitles:@"iOS7",nil];
    [alert show];
    NSLog(@"iOS7:%@", userInfo);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
//    NSNotification *notification = [NSNotification notificationWithName:@"notifition" object:self userInfo:userInfo];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:userInfo];
    
        NSDictionary *remoteNotification = userInfo[@"aps"];
    NSString *string = [NSString stringWithFormat:@"%d", [remoteNotification[@"badge"] intValue]];
    NSString *systemVersion = [NSString stringWithFormat:@"iOS %@", [UIDevice currentDevice].systemVersion];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:remoteNotification[@"alert"] message:string delegate:self cancelButtonTitle:remoteNotification[@"sound"] otherButtonTitles:systemVersion, nil];
    [alert show];
    NSLog(@"iOS8:%@", userInfo);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"==%@", error);
}
// 本地通知



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [APService setBadge:0];
    [application setApplicationIconBadgeNumber:0];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//    [APService setLocalNotification:[NSDate dateWithTimeIntervalSinceNow:1] alertBody:@"welcome back" badge:1 alertAction:@"0K" identifierKey:@"identifierKey" userInfo:nil soundName:nil];

}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
